import {commonApi} from "../../api/common";
import {countryCodeMap} from "../js/countryCodeMap";
import {mapGetters} from 'vuex'
import validateCode from 'base/validate-code/validate-code'

export default {
  components: {
    validateCode
  },
  computed: {
    ...mapGetters(['lang'])
  },
  mounted() {
    // console.log("mixin mounted", this.lang)
  },
  data() {
    return {
      countryCodeMap: countryCodeMap,
      loading: false,
    }
  },
  methods: {
    checkValidateCode(rule, value, callback) {
      if(value == "") {
        callback(new Error(this.$t('m.loginRegist.plzInputCode')));
      }else if (!value || !/^\d{6}/.test(value)) {
        callback(new Error(this.$t('m.loginRegist.errorCode')));
      } else {
        callback();
      }
    },
    checkAccout(rule, value, callback) {
      value = value.trim();
      if (value === '') {
        callback(new Error(this.$t('m.loginRegist.plzInputAccount')));
      } else {
        callback();
      }
    },
    checkMobile(rule, value, callback) {
      value = value.trim();
      let countryCode = this.ruleForm.countryCode;
      this.errors.mobile = '';
      if (value === '') {
        callback(new Error(this.$t('m.userCenter.plzPhoneNumber')));
      } else if (!/^\d+/.test(value)) {
        callback(new Error(this.$t('m.userCenter.phoneFormatError')));
      } else {
        let reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
        if (countryCode === '+86') {
          if (!reg.test(value)) {
            callback(new Error(this.$t('m.userCenter.phoneFormatError')));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    },
    checkEmail(rule, value, callback) {
      let mailReg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+(\.[a-zA-Z]{2,3})+$/;
      value = value.trim();
      if (value === '') {
        callback(new Error(this.$t('m.loginRegist.plzInputEmail')));
      } else if (!(mailReg.test(value))) {
        callback(new Error(this.$t('m.loginRegist.errorEmail')));
      } else {
        callback();
      }
    },
    checkPassword(rule, value, callback) {
      value = value.trim();
      if (value === '') {
        callback(new Error(this.$t('m.userCenter.plzPassword')));
      } else {
        callback()
      }
    },
    // 裁剪手机号
    slicePhoneNumber(num) {
      if (this.ruleForm.countryCode === '+86') {
        if (num.length > 11) {
          this.ruleForm.mobile = num.slice(0, 11);
        }
      }
    }
  }
}

